package com.qf.controller.manager;

import com.github.pagehelper.PageInfo;
import com.qf.entitys.Business;
import com.qf.entitys.BusinessWithBLOBs;
import com.qf.entitys.RespBean;
import com.qf.service.BusinessService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/*商家管理*/
@Controller
@RequestMapping("/manager/business")
public class BusinessController {

    @Resource
    private BusinessService service;

    @GetMapping("/list")
    public String list(Business record,Integer pageindex, HttpServletRequest request){
        PageInfo<BusinessWithBLOBs> businessPageInfo = service.selectAll(pageindex, record);
        request.setAttribute("pageInfo",businessPageInfo);
        request.setAttribute("bname",record.getBname());
        request.setAttribute("address",record.getAddress());
        return "manager/BusinessList";
    }

    @GetMapping("/updateBactive")
    @ResponseBody
    public RespBean updateBactive(Integer bid,Integer bactive){
        int i = service.updateBactive(bid, bactive);
        if (i>0){
            return RespBean.success("操作成功!");
        }else {
            return RespBean.error("操作失败!");
        }
    }

    /**
     * 批量审核通过或批量冻结
     * @return
     */
    @PostMapping("/updateBactive")
    @ResponseBody
    public RespBean updateBactiveSel(@RequestParam("ids[]")Integer[] ids,Integer bactive){
        try {
            int i = service.updateBactiveBySel(ids, bactive);
            if (i>0){
                return RespBean.success("操作成功!");
            }else {
                return RespBean.error("操作失败...");
            }
        }catch (Exception ex){
            return RespBean.error("操作失败...");
        }
    }

    @GetMapping("/detail")
    public String detail(Integer bid,HttpServletRequest request){
//        BusinessWithBLOBs businessWithBLOBs = service.selectByPrimaryKey(bid);
        BusinessWithBLOBs businessWithBLOBs = service.selectDetailByid(bid);
        request.setAttribute("b",businessWithBLOBs);
        return "manager/BusinessDetail";
    }

    @GetMapping("/noactive")
    public String noactive(Business record,Integer pageindex,HttpServletRequest request){
        record.setBactive(false);
        PageInfo<BusinessWithBLOBs> businessPageInfo = service.selectAll(pageindex, record);
        request.setAttribute("pageInfo",businessPageInfo);
        request.setAttribute("bname",record.getBname());
        request.setAttribute("address",record.getAddress());
        return "manager/BusinessListNoactive";
    }

    /**
     * 拒绝商户通过审核，意思是从数据库里把这个商户删掉
     * @param bid
     * @return
     */
    @GetMapping("/cancelBusiness")
    @ResponseBody
    public RespBean cancelBusiness(Integer bid){
        try {
            int i = service.deleteByPrimaryKey(bid);
            if (i>0){
                return RespBean.success("已拒绝!");
            }else {
                return RespBean.error("操作失败!");
            }
        }catch (Exception ex){
            return RespBean.error("操作失败!");
        }
    }

    /**
     * 批量拒绝商户的申请
     * @return
     */
    @GetMapping("/delbyids")
    @ResponseBody
    public RespBean delbyids(@RequestParam("ids[]")Integer[] ids){
        try {
            int delbyids = service.delbyids(ids);
            if (delbyids>0){
                return RespBean.success("操作成功!");
            }else{
                return RespBean.error("操作失败...");
            }
        }catch (Exception e){
            return RespBean.error("操作失败...");
        }
    }

}
